package com.example.easybuy.service;

import com.example.easybuy.dao.NewsDao;
import com.example.easybuy.pojo.News;
import com.example.easybuy.pojo.Page;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class NewsServiceImpl implements NewsService{

    private Logger logger= Logger.getLogger(getClass());

    @Autowired
    private NewsDao newsDao;


    /*
    查询新闻前五条
     */
    @Override
    public List<News> getNewsFive(Page page) {
        logger.debug("NewsServiceImpl-getNewsFive()...start...");
        //查询 ：总数据条数
        int totalDataCount=newsDao.getNewsAll();
        //页面容量
        page.setPageSize(5);
        //总数据条数
        page.setTotalDataCount(totalDataCount);

        //从第几条数据开始显示=(当前页码-1）*页面容量
        int pageNo=(page.getCurrentPageNo()-1)*page.getPageSize();
        page.setCurrentPageNo(pageNo);
        List<News> newsList=newsDao.getNewsFive(page);
        logger.debug("NewsServiceImpl-getNewsFive()-newsList："+newsList.toString());
        return newsList;
    }

    /*
    根据id查询新闻
     */
    @Override
    public News getNewsOne(String id) {
        logger.debug("NewsServiceImpl-getNewsOne()...start...id:"+id);
        News news=newsDao.getNewsOne(id);
        logger.debug("NewsServiceImpl-getNewsOne()-news:"+news.toString());
        return news;
    }

    /*
    删除新闻
     */
    @Override
    public String deletNews(String id) {
        logger.debug("NewsServiceImpl-deletNews()...start...id:"+id);
        int deletRow=newsDao.deletNews(id);
        String success=null;
        if (deletRow>0){
            success="删除成功";
        }else{
            success="删除失败";
        }
        return success;
    }

    /*
    添加新闻
     */
    @Override
    public void addNews(News news) {
        logger.debug("NewsServiceImpl-addNews()...start...news:"+news);
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd-HH");
        String nowTime=df.format(new Date());
        news.setCreateTime(nowTime);
        news.setIdDelet(0);
        newsDao.addNews(news);
    }

    /*
    根据id修改新闻
     */
    @Override
    public void modifyNews(News news) {
        logger.debug("NewsServiceImpl-modifyNews()...start...news:"+news);
        newsDao.modifyNews(news);
    }
}
